
#include <BFont>
#include <BImage>
#include <BPainter>

#include "PaintWidget.h"

PaintWidget::PaintWidget(BWidget* parent) : BWidget(parent)
{

}
PaintWidget::~PaintWidget()
{

}

void PaintWidget::resizeEvent(const BEvent& event)
{
	BWidget::resizeEvent(event);
}
float globalAngle = 0;
void PaintWidget::updateEvent(const BEvent& event)
{
	BWidget::updateEvent(event);
	globalAngle += 10;
	if (globalAngle > 360)
		globalAngle -= 360;
	this->fresh();
}

void PaintWidget::styleEvent(const BEvent& event)
{
	BWidget::styleEvent(event);
}
void PaintWidget::freshEvent(const BEvent& event)
{
	BWidget::freshEvent(event);
}
void PaintWidget::paintEvent(const BEvent& event)
{
	BWidget::paintEvent(event);

	BRect crect = this->centerRect();
	BPainter painter(this);

	painter.setColor(Color_Foreground);
	painter.fillRect(crect);

	painter.setPointSmooth(true);
	painter.setPointSize(10);
	painter.setColor(0, 0, 0);
	painter.drawPoint(30, 160);

	painter.setColor(Color_Text);
	painter.drawText(crect, "This is text.");

	painter.setColor(0, 200, 100);
	painter.setPolySmooth(true);
	painter.fillArc(crect.center(), BRange(30, 80), 0, 90);

	painter.setColor(150, 200, 50);
	painter.fillChord(crect.center(), 100, 100, 200);

	painter.fillPie(crect.center(), 60, 180, 300);

	static BImage* image = new BImage("background.png");
	painter.drawImage(crect.align(Align_Center, 100), image, BMargin(15));

	painter.setColor(0, 0, 0, 255);
	painter.drawGraph(20, 20, 20, 20, Graph_Arrow_Up_Fill);

	static BPointfArray points;
	points.reset();

	int sides = 100;
	float angle = globalAngle;
	float radius = 100;
	float rstep = radius / sides;
	while (sides-- > 0)
	{
		float radian = bDTR(angle);
		float x = cos(radian) * radius;
		float y = sin(radian) * radius;
		points.append().set(x, y);

		angle += 10;
		radius -= rstep;
	}
	BPointf& last = points.last();
	painter.setLineWidth(3);
	painter.setLineSmooth(true);
	painter.translate(100, 100);
	painter.drawLineStrip(points);
}
